package com.webc.framework.bean;

import com.webc.framework.annotation.Aspect;
import com.webc.framework.annotation.Before;
import com.webc.framework.annotation.PointCut;
import com.webc.framework.start.LaunchClassloader;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author hejianglong
 * @date 2018/9/8
 */
public class AopContainer {

    private static final List<AopHandler> ASPECT_CONTAINER = new ArrayList<>();

    static {
        initAspect();
    }

    private static void initAspect() {
        List<Class<?>> classes = LaunchClassloader.getClasses();
        classes.stream()
                .filter(cl -> cl.isAnnotationPresent(Aspect.class))
                .forEach(aClass -> {
                    Method[] methods = aClass.getClass().getDeclaredMethods();
                    Arrays.asList(methods).forEach(method -> {
                        if(method.getAnnotation(Before.class) != null) {
                            PointCut pointCut = method.getAnnotation(PointCut.class);
                            if (pointCut == null) {
                                throw new RuntimeException("必须设置pointCut: " + method.getName());
                            }
                            ASPECT_CONTAINER.add(new AopHandler(Before.class, method, pointCut.value()));
                        }
                    });
                });
    }

}
